package eteam.aps.gateway.controller;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Maps;
import eteam.aps.common.QueryResult;
import eteam.aps.common.R;
import eteam.aps.gateway.client.SystemAuthFeignClient;
import eteam.aps.gateway.client.SystemLogFeignClient;
import eteam.aps.gateway.config.GatewayConfig;
import eteam.aps.gateway.config.ModuleProperty;
import eteam.aps.gateway.config.TestParamConfig;
import eteam.aps.gateway.config.TestParamProperty;
import eteam.aps.systeminterface.model.dto.LogDto;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import eteam.aps.systeminterface.model.entity.LogEntity;
import eteam.aps.systeminterface.model.reqparam.ResetPwdParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

@RestController
//@EnableConfigurationProperties(ModuleProperty.class)
@RequestMapping(value = "/api")
public class TestController {
    @Autowired
    private TestParamConfig testParamConfig;
    @Autowired
    private TestParamProperty testParamProperty;

    @Autowired
    private GatewayConfig gatewayConfig;
    @Autowired
    private ModuleProperty moduleProperty;
    @Autowired
    private SystemAuthFeignClient authFeignClient;
    @Autowired
    private SystemLogFeignClient logFeignClient;

    @RequestMapping(value = "/getnow", method = RequestMethod.GET)
    public R<String> getNow(@RequestHeader(value = "token", required = false) String token) {
        Map<String, Object> map = Maps.newHashMap();
        try {
            map.put("abc", testParamConfig.getParam1());
            map.put("def", testParamProperty.getParam3());
            map.put("url", gatewayConfig.getSystemUrl());
            map.put("modules", moduleProperty.getModules());
            map.put("excludeurls", moduleProperty.getExcludeUrls());

            //测试调用System服务
            ResetPwdParam pa = new ResetPwdParam();
            pa.setUserid("aaaa");
            pa.setNewpwd("1231");
            map.put("msg", "");
            map.put("a", authFeignClient.getNow());
            map.put("b", authFeignClient.getNow01("ab"));
            map.put("c", authFeignClient.postInfo(pa));
            R<VerifyTokenDto> result = authFeignClient.verifyToken(token);
            map.put("result", result);
            if (!Objects.isNull(result) && result.getCode() != 200) {
                map.put("msg", result.getMsg());
            }

            //测试查询日志
            R<QueryResult<LogEntity>> queryRet = logFeignClient.queryLogPageList(token, 1, 10, "87465ed3080911eca29768b599cb032e");
            if (!Objects.isNull(queryRet) && queryRet.getCode() != 200) {
                map.put("msg", result.getMsg());
            }

            //测试记录日志
            R logRet = createLog(token);
            if (!Objects.isNull(logRet) && logRet.getCode() != 200) {
                map.put("msg", result.getMsg());
            }
        } catch (Exception e) {
            map.put("msg", "：" + e.getMessage());
            e.printStackTrace();
        }
        return R.success(DateUtil.now() + map.get("msg"));
    }

    private R createLog(String token) throws Exception {
        LogDto tmpLog = new LogDto();
        tmpLog.setFmoduleid("87465ed3080911eca29768b599cb032e");
        tmpLog.setFmodulename("客户信息");
        tmpLog.setFoper("customer_add");
        tmpLog.setFopername("新增客户");
        tmpLog.setFdesc("日志描述，干了些什么");
        return logFeignClient.createLog(token, tmpLog);
    }
}
